package loppy.dynamics 
{
  import loppy.loppy_internal;
  import loppy.math.Vec2;
  import loppy.utils.InListNode;
  
  public class Shape extends InListNode
  {
    use namespace loppy_internal;
    
    public static const CIRCLE :uint = 0x1;
    public static const POLYGON:uint = 0x2;
    
    private static var idCounter:uint = 0;
    loppy_internal var id       :uint = 0;
    loppy_internal var type     :uint = 0;
    
    loppy_internal var parent:Body  = null;
    
    loppy_internal var localPosition  :Vec2   = new Vec2();
    loppy_internal var globalPosition :Vec2   = new Vec2();
    loppy_internal var localRotation  :Number = 0.0;
    loppy_internal var globalRotation :Number = 0.0;
    
    public         var friction       :Number;
    public         var restitution    :Number;
    
    loppy_internal var config         :ShapeConfig;
    loppy_internal var density        :Number = 1.0;
    loppy_internal var mass           :Number = 0.0;
    loppy_internal var inverseMass    :Number = Number.POSITIVE_INFINITY;
    loppy_internal var intertia       :Number = 0.0;
    loppy_internal var inverseInertia :Number = Number.POSITIVE_INFINITY;
    loppy_internal var proxy          :Proxy = new Proxy();
    
    loppy_internal var userData:* = null;
    public function getUserData():* { return userData; }
    public function setUserData(data:*):Shape
    {
      userData = data;
      return this;
    }
    
    public function Shape(type:uint, config:ShapeConfig)
    {
      id = idCounter++;
      this.type    = type;
      this.config  = config;
      proxy.parent = this;
      
      localPosition.copy(config.localPosition);
      localRotation    = config.localRotation;
      friction         = config.friction;
      restitution      = config.restitution;
      density          = config.density;
    }
    
    internal function updateMass():void
    { }
    
    internal function updateTransform():void
    {
      globalPosition
        .copy(localPosition)
        .rotateThis(parent.rotation)
        .addThis(parent.position);
      globalRotation = parent.rotation + localRotation;
    }
    
    internal function updateProxy():void
    { }
    
    public function toString():String
    {
      return "[Shape id=" + id + "]";
    }
  }
}
